package org.springframework.aop.framework;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Before;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectInstanceFactory;
import org.springframework.aop.aspectj.AspectJAfterReturningAdvice;
import org.springframework.aop.aspectj.AspectJAroundAdvice;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.aspectj.AspectJMethodBeforeAdvice;
import org.springframework.aop.aspectj.SingletonAspectInstanceFactory;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.framework.ReflectiveMethodInvocation;
import org.springframework.aop.interceptor.ExposeInvocationInterceptor;
import org.springframework.aop.support.DefaultPointcutAdvisor;


/*****
 *  注意包名
 * @author Administrator
 *
 */
public class A18 {
    public static void main(String[] args) throws  Throwable {
    	test3();
    }
    /****
     *  执行调用链
     * @throws Throwable 
     */
	public static void test3() throws  Throwable {
		 AspectInstanceFactory factory = new SingletonAspectInstanceFactory(new Aspect());
	        // 高级切面转低级切面类
	        List<Advisor> list = new ArrayList<>();
	        for (Method method : Aspect.class.getDeclaredMethods()) {//通过切面类得到里边的方法
	            if (method.isAnnotationPresent(Before.class)) {//判断是否有@before的注解
	                // 解析切点
	                String expression = method.getAnnotation(Before.class).value();//得到注解表达式
	                AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
	                pointcut.setExpression(expression);//创建切点类 把表达式设置进去
	                //  创建通知类
	                AspectJMethodBeforeAdvice advice = new AspectJMethodBeforeAdvice(method, pointcut, factory);
	                // 创建切面
	                Advisor advisor = new DefaultPointcutAdvisor(pointcut, advice);
	                list.add(advisor);
	            }else if (method.isAnnotationPresent(AfterReturning.class)) {
	                // 解析切点
	                String expression = method.getAnnotation(AfterReturning.class).value();
	                AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
	                pointcut.setExpression(expression);
	                // 通知类
	                AspectJAfterReturningAdvice advice = new AspectJAfterReturningAdvice(method, pointcut, factory);
	                // 切面
	                Advisor advisor = new DefaultPointcutAdvisor(pointcut, advice);
	                list.add(advisor);
	            } else if (method.isAnnotationPresent(Around.class)) {
	                // 解析切点
	                String expression = method.getAnnotation(Around.class).value();
	                AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
	                pointcut.setExpression(expression);
	                // 通知类
	                AspectJAroundAdvice advice = new AspectJAroundAdvice(method, pointcut, factory);
	                // 切面
	                Advisor advisor = new DefaultPointcutAdvisor(pointcut, advice);
	                list.add(advisor);
	            }
	        }
	        for (Advisor advisor : list) {
	            System.out.println("----"+advisor);
	        }
	        
	        
	        Target target = new Target();
	        ProxyFactory proxyFactory = new ProxyFactory();
	        proxyFactory.setTarget(target);
	        proxyFactory.addAdvice(ExposeInvocationInterceptor.INSTANCE); // 准备把 MethodInvocation 放入当前线程  如果不添加，执行调用链报错
	        proxyFactory.addAdvisors(list);

	        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
	        List<Object> methodInterceptorList = proxyFactory.getInterceptorsAndDynamicInterceptionAdvice(Target.class.getMethod("foo"), Target.class);
	       // getInterceptorsAndDynamicInterceptionAdvice方法把非环绕通知的转换成环绕通知
	        for (Object o : methodInterceptorList) {
	            System.out.println(o);
	        }
	        
	        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
	        // 3. 创建并执行调用链 (环绕通知s + 目标)
	        MethodInvocation methodInvocation = new ReflectiveMethodInvocation(null, target, Target.class.getMethod("foo"), new Object[0], Target.class, methodInterceptorList);
	        methodInvocation.proceed();

	}
    
    /****
     * 把非环绕通知的，转换成环绕通知
     * @throws Exception
     */
	public static void test2() throws  Exception {
		 AspectInstanceFactory factory = new SingletonAspectInstanceFactory(new Aspect());
	        // 高级切面转低级切面类
	        List<Advisor> list = new ArrayList<>();
	        for (Method method : Aspect.class.getDeclaredMethods()) {//通过切面类得到里边的方法
	            if (method.isAnnotationPresent(Before.class)) {//判断是否有@before的注解
	                // 解析切点
	                String expression = method.getAnnotation(Before.class).value();//得到注解表达式
	                AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
	                pointcut.setExpression(expression);//创建切点类 把表达式设置进去
	                //  创建通知类
	                AspectJMethodBeforeAdvice advice = new AspectJMethodBeforeAdvice(method, pointcut, factory);
	                // 创建切面
	                Advisor advisor = new DefaultPointcutAdvisor(pointcut, advice);
	                list.add(advisor);
	            }else if (method.isAnnotationPresent(AfterReturning.class)) {
	                // 解析切点
	                String expression = method.getAnnotation(AfterReturning.class).value();
	                AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
	                pointcut.setExpression(expression);
	                // 通知类
	                AspectJAfterReturningAdvice advice = new AspectJAfterReturningAdvice(method, pointcut, factory);
	                // 切面
	                Advisor advisor = new DefaultPointcutAdvisor(pointcut, advice);
	                list.add(advisor);
	            } else if (method.isAnnotationPresent(Around.class)) {
	                // 解析切点
	                String expression = method.getAnnotation(Around.class).value();
	                AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
	                pointcut.setExpression(expression);
	                // 通知类
	                AspectJAroundAdvice advice = new AspectJAroundAdvice(method, pointcut, factory);
	                // 切面
	                Advisor advisor = new DefaultPointcutAdvisor(pointcut, advice);
	                list.add(advisor);
	            }
	        }
	        for (Advisor advisor : list) {
	            System.out.println("----"+advisor);
	        }
	        
	        
	        Target target = new Target();
	        ProxyFactory proxyFactory = new ProxyFactory();
	        proxyFactory.setTarget(target);
	        proxyFactory.addAdvisors(list);

	        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
	        List<Object> methodInterceptorList = proxyFactory.getInterceptorsAndDynamicInterceptionAdvice(Target.class.getMethod("foo"), Target.class);
	       // getInterceptorsAndDynamicInterceptionAdvice方法把非环绕通知的转换成环绕通知
	        for (Object o : methodInterceptorList) {
	            System.out.println(o);
	        }

	}
	
	
    static class Aspect {
        @Before("execution(* foo())")
        public void before1() {
            System.out.println("before1");
        }

        @Before("execution(* foo())")
        public void before2() {
            System.out.println("before2");
        }

        public void after() {
            System.out.println("after");
        }

        @AfterReturning("execution(* foo())")
        public void afterReturning() {
            System.out.println("afterReturning");
        }

        @AfterThrowing("execution(* foo())")
        public void afterThrowing(Exception e) {
            System.out.println("afterThrowing " + e.getMessage());
        }

        @Around("execution(* foo())")
        public Object around(ProceedingJoinPoint pjp) throws Throwable {
            try {
                System.out.println("around...before");
                return pjp.proceed();
            } finally {
                System.out.println("around...after");
            }
        }
    } 

    static class Target {
        public void foo() {
            System.out.println("target foo");
        }
    }

    
}